package com.core;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Service {
	
	private static Regs regs;
	static {
		regs = new Regs();
	}
	
	/**
	 * <p>Title: getInfo</p>
	 * <p>Description: </p>
	 * @param regName ： 正则表达式在com.core.Regs中对应的属性名称
	 * @param str：		被处理的字符串
	 * @return	String：
	 * 				找不到相应的子串：	null
	 * 				能找到对应的子串：	用`分割的“value”。（数据文件中不存在`）
	 * @throws Exception
	 */
	public String getInfo(String regName, String str) throws Exception {
		Pattern pattern = regs.get(regName);
		Matcher matcher = pattern.matcher(str);
		String res = "";
		while(matcher.find()) {
			String temp = matcher.group(0).split("=")[1];
			if(temp != null && !"null".equals(temp))
				res += "`" + temp;
		}
		return res.equals("") ? null : res.substring(1, res.length());
	}
	
	/**
	 * <p>Title: getInfo</p>
	 * <p>Description: </p>
	 * @param regName ： 正则表达式在com.core.Regs中对应的属性名称
	 * @param str：		被处理的字符串
	 * @return	String：
	 * 				找不到相应的子串：	null
	 * 				能找到对应的子串：	用`分割的“key=value”。（数据文件中不存在`）
	 * @throws Exception
	 */
	public String getFullInfo(String regName, String str) throws Exception {
		Pattern pattern = regs.get(regName);
		Matcher matcher = pattern.matcher(str);
		String res = "";
		while(matcher.find()) {
			res += "`" + matcher.group(0);
		}
		return res.equals("") ? null : res.substring(1, res.length());
	}
	/**
	 * <p>Title: printInfos</p>
	 * <p>Description: 输出map信息</p>
	 * @param map
	 */
	public void printInfos(Map<String, PortGuidance> map) {
		boolean flag = true;
		Set<String> keySet = map.keySet();
		if (keySet.size() != 0) {
			Iterator<String> iterator = keySet.iterator();
			while (iterator.hasNext()) {
				PortGuidance guidance = map.get(iterator.next());
				if (guidance != null && guidance.getFlno() != null && guidance.getSfno() != null 
						&& guidance.getTarget() != null && guidance.getCode() != null) {
					String sfno = guidance.getSfno().replaceAll("`", "、");
					int length = sfno.length();
					if(length > 8) {
						sfno += "\t";
					}else
						sfno += "\t\t";
					if(flag) {
						System.out.println("\n\n*********************************************************************");
						System.out.println("航班号\t共享航班号 " + (length>8? "\t\t":  "\t\t") + "经停站\t目的地\t预计起飞\t实际起飞\t航班状态\t值班柜台");
						flag = false;
					}
					
					System.out.print(guidance.getFlno() + "\t" + sfno + 
							guidance.getStopover() + "\t" + guidance.getTarget() + "\t");

					String fett = guidance.getFett();
					if(fett != null)
						System.out.print(fett.substring(4, 6) + "月" + fett.substring(6, 8) + 
								"日" + fett.substring(8, 10) + "时" + fett.substring(10, 12) + "分\t");
					else
						System.out.print("未知\t");
					
					String frtt = guidance.getFrtt();
					if(frtt != null)
						System.out.print(frtt.substring(4, 6) + "月" + frtt.substring(6, 8) + 
								"日" + frtt.substring(8, 10) + "时" + frtt.substring(10, 12) + "分\t");
					else
						System.out.print("未知\t");
					String code = guidance.getCode();
					if(code == null || "null".equals(code))
						code = "未知";
					else
						code = code.replaceAll("`", "、");
					System.out.println(guidance.getStatus() + "\t" + code);
				}
			}
		}
	}
	
	// 截取消息类型
	public String getType(String str) {
		return str.substring(20, 29);
	}
	
	private static final String sep = "+";
	
	/**
	 * <p>Title: handleResultFromMapToList</p>
	 * <p>Description: 把Map处理成List</p>
	 * @param map
	 * @param flag  true:进行数据过滤   false:不进行数据过滤
	 * @return
	 */
	public List<String> handleResultFromMapToList(Map<String, PortGuidance> map, boolean flag){
		List<String> list = new LinkedList<>();
		Set<String> set = map.keySet();
		Iterator<String> iterator = set.iterator();
		while(iterator.hasNext()) {
			PortGuidance g = map.get(iterator.next());
			String str = g.getFlno() + sep + g.getSfno() + sep + g.getStopover() + sep 
					+ g.getTarget() + sep + g.getFett() + sep + g.getFrtt() + sep +g.getStatus() + sep + g.getCode();
			//flag == false：永远为真，不过滤数据
			//flag == true ：check()决定
			if(check(str) || !flag)
				list.add(str.replace('`', '和'));
		}
		return list;
	}
	
	private Pattern compile = Pattern.compile("null");
	
	private boolean check(String line) {
		int i = 0;
		Matcher matcher = compile.matcher(line);
		while(matcher.find()) {
			i++;
		}
		return i > 3 ? false : true;
	}
	
}